SUBROUTINE READ_RASS_DATA(I4TIME_SYS,CYCLE_TIME,MAX_RASS,MAX_LEVELS,RVALUE_MISSING, &
                            I4TIME_OBS,NUM_RASS,LVL_RASS,C5_NAMES,C8_TYPE, &
                            LAT_RASS,LON_RASS,ELV_RASS,ERR_RASS, &
                            HGT_RASS,TMP_RASS,ISTATUS)

!==============================================================================
!doc  THIS ROUTINE READS RASS RAW DATA FROM LAPS LRS FILES.
!doc  IT IS THE SAME AS THE FIRST PART OF READ_TSND.F
!doc
!doc  HISTORY:
!doc	CREATION:	YUANFU XIE	JAN. 2009
!==============================================================================

  IMPLICIT NONE

  INTEGER, INTENT(IN) :: I4TIME_SYS,CYCLE_TIME,MAX_RASS,MAX_LEVELS
  REAL,    INTENT(IN) :: RVALUE_MISSING

  CHARACTER, INTENT(OUT) :: C5_NAMES(MAX_RASS)*5,C8_TYPE(MAX_RASS)*8
  INTEGER,   INTENT(OUT) :: NUM_RASS,LVL_RASS(MAX_RASS),I4TIME_OBS(MAX_RASS)
  REAL,      INTENT(OUT) :: LAT_RASS(MAX_RASS),LON_RASS(MAX_RASS), &
                            ELV_RASS(MAX_RASS),ERR_RASS(MAX_RASS), &
                            HGT_RASS(MAX_RASS,MAX_LEVELS), &		! M
                            TMP_RASS(MAX_RASS,MAX_LEVELS)		! C

  ! LOCAL VARIABLES:
  CHARACTER :: EXT*3, C_FILESPEC*255,A9TIME*9
  INTEGER   :: I4TIME_FILE,LAG_TIME,I4TIME_RASS_OFFSET, &
               STTNID,I_QC,I_RASS,I_LEVEL,N_LEVEL,ISTATUS
  LOGICAL   :: L_STRING_CONTAINS
  REAL      :: RCYCLES
  REAL,PARAMETER :: SURFACE_RASS_BUFFER = 30.0
  
  EXT = 'lrs'
  CALL GET_FILESPEC(EXT,2,C_FILESPEC,ISTATUS)
  CALL GET_FILE_TIME(C_FILESPEC,I4TIME_SYS,I4TIME_FILE)

  LAG_TIME = 0 ! Middle of rass hourly sampling period
  I4TIME_RASS_OFFSET = I4TIME_SYS - (I4TIME_FILE + LAG_TIME)
  RCYCLES = FLOAT(I4TIME_RASS_OFFSET) / FLOAT(CYCLE_TIME)

  WRITE(6,*)' i4time_rass_offset/rcycles = ',i4time_rass_offset,rcycles

  ! INITIALIZATION:
  NUM_RASS = 0
  LVL_RASS = 0

  IF (ABS(I4TIME_RASS_OFFSET) .gt. CYCLE_TIME) THEN
    WRITE(6,*)' RASS file/offset is > LAPS cycle time'
    WRITE(6,*)' Skipping the use of RASS'
    RETURN
  ENDIF

  CALL OPEN_LAPSPRD_FILE_READ(12,I4TIME_FILE,EXT,ISTATUS)
  IF (ISTATUS .NE. 1) THEN
    PRINT*,'Read_RASS_Data: cannot open data file: ',EXT
    RETURN
  ENDIF

  DO I_RASS = 1,MAX_RASS

    NUM_RASS = NUM_RASS+1	! COUNT

340 CONTINUE

    READ(12,401,end=500) STTNID,N_LEVEL, &
	LAT_RASS(NUM_RASS),LON_RASS(NUM_RASS),ELV_RASS(NUM_RASS), &
        C5_NAMES(NUM_RASS),A9TIME,C8_TYPE(NUM_RASS)
401 FORMAT(i12,i12,f11.0,f15.0,f15.0,5x,a5,3x,a9,1x,a8)

    ! CONVERT A9TIME TO I4TIME:
    CALL CV_ASC_I4TIME(A9TIME,I4TIME_OBS(NUM_RASS),ISTATUS)

    IF (N_LEVEL .gt. MAX_LEVELS) THEN
      WRITE(6,*)'Read_RASS_Data ERROR: too many levels in the LRS file: ', &
                I_RASS,N_LEVEL,MAX_LEVELS
      ISTATUS = 0
      RETURN
    ENDIF

    IF (L_STRING_CONTAINS(C8_TYPE(NUM_RASS),'SAT',ISTATUS)) THEN
       ERR_RASS(NUM_RASS) = 5.0
    ELSE
       ERR_RASS(NUM_RASS) = 1.0
    ENDIF

    DO I_LEVEL = 1,N_LEVEL

      READ(12,*,ERR=340) HGT_RASS(NUM_RASS,I_LEVEL),TMP_RASS(NUM_RASS,I_LEVEL),I_QC

      ! SUPPOSE LRS FILES CONTAIN TEMPERATURE IN KELVIN:
      IF (I_QC .EQ. 1 .AND. &
          HGT_RASS(NUM_RASS,I_LEVEL) .LT. RVALUE_MISSING .AND. &
          TMP_RASS(NUM_RASS,I_LEVEL) .GT. 200.0 .AND. &
          TMP_RASS(NUM_RASS,I_LEVEL) .LT. 450.0 .AND. &
          HGT_RASS(NUM_RASS,I_LEVEL) .GT. ELV_RASS(NUM_RASS)+SURFACE_RASS_BUFFER .AND. &
          I_LEVEL .LE. MAX_LEVELS ) THEN

        LVL_RASS(NUM_RASS) = LVL_RASS(NUM_RASS) + 1		! COUNT LEVELS

        HGT_RASS(NUM_RASS,LVL_RASS(NUM_RASS)) = HGT_RASS(NUM_RASS,I_LEVEL)
        TMP_RASS(NUM_RASS,LVL_RASS(NUM_RASS)) = TMP_RASS(NUM_RASS,I_LEVEL)-273.15	! IN CENTIGRADE

      ENDIF

    ENDDO ! LEVEL

    IF (LVL_RASS(NUM_RASS) .EQ. 0) NUM_RASS=NUM_RASS-1

  ENDDO ! RASS

500 CONTINUE
  NUM_RASS=NUM_RASS-1	! WHEN REACHING THE END OF FILE, NUM_RASS HAS BEEN ADDED

  CLOSE(12)	! CLOSE LRS FILE

END SUBROUTINE READ_RASS_DATA
